package com.wy.controller;
import com.wy.pojo.User;
import com.wy.service.UserService;

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;

import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * @Author: wy
 * @Date:2019/10/26 18:42
 */

@RestController
public class UserController {

      @Autowired
       private UserService userService;

       @GetMapping("/findAll")
     public List<User> findAll(){

          return  userService.findAll();
     }


       /*//全文检索（根据整个实体的所有属性，可能结果为0个）
       @GetMapping("/findUsers/{name}")
     public List<User> testSearch(@PathVariable String name){

           QueryStringQueryBuilder builder = new QueryStringQueryBuilder(name);
           Iterable<User> searchResult = esRepository.search(builder);
           Iterator<User> iterator = searchResult.iterator();
           List<User> list = new ArrayList<User>();
           while (iterator.hasNext()) {
               list.add(iterator.next());
           }
           return list;
     }

      //：分页、分数、分域（结果一个也不少）根据年龄查询
    @GetMapping("/select/{page}/{size}/{name}")
      public List<User> pageSearch(@PathVariable Integer page,@PathVariable Integer size,@PathVariable String name ){
          // 分页参数
          Pageable pageable = new PageRequest(page, size);

        // 分数，并自动按分排序
        FunctionScoreQueryBuilder functionScoreQueryBuilder1 =QueryBuilders.functionScoreQuery(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("name",name)),
                ScoreFunctionBuilders.weightFactorFunction(1000));// 权重：name 1000分
        FunctionScoreQueryBuilder functionScoreQueryBuilder2 =QueryBuilders.functionScoreQuery(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("userName", name)),
                        ScoreFunctionBuilders.weightFactorFunction(100)); // 权重：message 100分
        // 分数、分页
          SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable)
                  .withQuery(functionScoreQueryBuilder1).withQuery(functionScoreQueryBuilder2).build();
        Page<User> searchPageResults = esRepository.search(searchQuery);
        return searchPageResults.getContent();
      }

      //增加
     @PostMapping("/insert")
      public  User insertUser(User user){
         esRepository.save(user);
           return  user;
      }

      //删除
      @DeleteMapping("/delete/{id}")
      public User insertBook(@PathVariable String id) {
          Optional<User> user = esRepository.findById(id);
          esRepository.delete(user.get());
          return user.get();
      }

           @PostMapping("/update")
       public User update(User user){
               esRepository.save(user);
             return user;
       }*/

}
